package com.rongshu.view.discovery.presenter;

import android.content.Context;
import android.content.Intent;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.discovery.activity.SearchResultActivity;
import com.rongshu.view.discovery.contract.SearchView;
import com.rongshu.view.home.bean.LabelResponse;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class SearchPresenter extends BasePresenter<SearchView> {
    private List<String> historyList = new ArrayList<>();

    public SearchPresenter(Context context) {
        super(context);
    }

    //标签类型(1.个人分类 2.笔友会分类 3.文章分类 4.关键字)
    public void getLabel() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("type", 1);
        RetorfitService.getSearchRecommendLabel(UserInfoUtils.getUserId(), 1,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<LabelResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(LabelResponse object) {
                if (object != null) {
                    getView().callBackTags(object.getResponse());
                }
            }
        });
    }

    public void saveHistory(List<String> msg) {
        String string = SPUtils.arrayList2String(msg);
        SPUtils.putPreferences(SPKey.SEARCH_HISTORY, string);
    }

    public List<String> getHistory() {
        String preferences = SPUtils.getPreferences(SPKey.SEARCH_HISTORY, "");
        return SPUtils.string2ArrayList(preferences);
    }

    public void clearHistory() {
        SPUtils.putPreferences(SPKey.SEARCH_HISTORY, "");
    }

    public void toSearchResult(LabelResponse.ResponseBean bean) {
        Intent intent = new Intent();
        intent.setClass(mContext, SearchResultActivity.class);
        intent.putExtra("searchMsg", bean);
        mContext.startActivity(intent);
    }

    public List<String> sortList(List<String> list) {
        historyList.clear();
        if (list != null) {
            for (int i = list.size() - 1; i >= 0; i--) {
                historyList.add(list.get(i));
            }
        }
        return historyList;
    }
}
